<!DOCTYPE html >
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
    <title>修改密码</title>
    <script th:replace="common/head::static"></script>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">修改密码</div>
                <div class="layui-card-body" pad15>
                    <!-- https://www.layui.com/doc/modules/form.html -->
                    <form class="layui-form">
                        <div class="layui-form-item">
                            <label class="layui-form-label">当前密码</label>
                            <div class="layui-input-inline">
                                <input type="password" name="oldPassword" lay-verify="required" lay-verType="tips" id="oriole-oldPassword" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">新密码</label>
                            <div class="layui-input-inline">
                                <input type="password" name="password" lay-verify="pass" lay-verType="tips" autocomplete="off" id="oriole-password" class="layui-input">
                            </div>
                            <div class="layui-form-mid layui-word-aux">6到16个字符</div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">确认新密码</label>
                            <div class="layui-input-inline">
                                <input type="password" name="rePassword" lay-verify="repass" lay-verType="tips" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button type="submit" class="layui-btn" lay-submit="" lay-filter="changePassword">立即提交</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script th:src="@{/common/js/common.js}"></script>
<script>
    layui.use(['form'], function () {
        const $ = layui.jquery,
            layer = layui.layer,
            form = layui.form;
        form.render();
        const $body = $('body');

        //自定义验证
        form.verify({
            //我们既支持上述函数式的方式，也支持下述数组的形式
            //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
            pass: [
                /^[\S]{6,16}$/
                ,'密码必须6到16位，且不能出现空格'
            ]
            //确认密码
            ,repass: function(value){
                if(value !== $('#oriole-password').val()){
                    return '两次密码输入不一致';
                }
                if(value === $('#oriole-oldPassword').val()){
                    return '新密码和旧密码不能相同';
                }
            }
        });

        form.on('submit(changePassword)', function (obj) {
            Common.ajaxPut(ctx + "/sysUser/updatePassword", obj.field, function (res){
                if (res.value === 200) {
                    layer.msg(res.message, {icon: 1, time: 1000});
                } else {
                    layer.msg(res.message, {icon: 2, time: 2000});
                }
            })
            return false;
        });
    });
</script>
</body>
</html>